import { create } from 'zustand';

import datasetService from '@/api/services/datasetService';
import knowService from '@/api/services/knowService';
import {
  AutoCorrectListType,
  AutoCorrectTrunksType,
  OptimizedistType,
  RepetitionType,
} from '@/types/know/know';

interface KnowledgeBaseApprovalState {
  showTitle: Boolean;
  setShowTitle: (value: Boolean) => void;
  radioValue: Boolean;
  setRadioValue: (value: Boolean) => void;
  isButtonShow: Boolean;
  setIsButtonShow: (value: Boolean) => void;
  checkoutList: OptimizedistType[];
  setCheckoutList: (value: OptimizedistType[]) => void;
  wronglyDataList: {
    auto_correct_pairs_with_count: AutoCorrectListType[];
    trunks: AutoCorrectTrunksType[];
  };
  setWronglyDataList: (value: {
    auto_correct_pairs_with_count: AutoCorrectListType[];
    trunks: AutoCorrectTrunksType[];
  }) => void;
  sensitiveDataList: {
    auto_correct_pairs_with_count: AutoCorrectListType[];
    trunks: AutoCorrectTrunksType[];
  };
  setSensitiveDataList: (value: {
    auto_correct_pairs_with_count: AutoCorrectListType[];
    trunks: AutoCorrectTrunksType[];
  }) => void;
  repetitionDataList: RepetitionType[];
  setRepetitionDataList: (value: RepetitionType[]) => void;

  fetchCheckoutList: (appId: string, collectionId: string) => void;
}
export const KnowledgeBaseApprovalStore = create<KnowledgeBaseApprovalState>((set) => ({
  showTitle: false,
  setShowTitle: (value) => set(() => ({ showTitle: value })),
  radioValue: false,
  setRadioValue: (value) => set(() => ({ radioValue: value })),
  isButtonShow: false,
  setIsButtonShow: (value) => set(() => ({ isButtonShow: value })),
  // 发请求
  checkoutList: [],
  setCheckoutList: (value) => set(() => ({ checkoutList: value })),
  wronglyDataList: {
    auto_correct_pairs_with_count: [],
    trunks: [],
  },
  setWronglyDataList: (value) => set(() => ({ wronglyDataList: value })),
  sensitiveDataList: {
    auto_correct_pairs_with_count: [],
    trunks: [],
  },
  setSensitiveDataList: (value) => set(() => ({ sensitiveDataList: value })),
  repetitionDataList: [],
  setRepetitionDataList: (value) => set(() => ({ repetitionDataList: value })),

  fetchCheckoutList: async (appId: string, collectionId: string) => {
    try {
      const res: any = await datasetService.getCheckList({ appId, collectionId });
      const list: OptimizedistType[] = (res?.list || res?.data || res || []) as OptimizedistType[];
      // 始终同步最新的列表到 checkoutList，避免下游读取到空
      set(() => ({ checkoutList: list }));

      // 空列表直接返回，避免访问 list[0]
      if (!Array.isArray(list) || list.length === 0) {
        return;
      }

      const typeArr = list[0]?.optimize_text;
      if (Array.isArray(typeArr) && typeArr.length > 1) {
        const res1 = await knowService.autoCorrectList(collectionId, appId);
        set(() => ({ wronglyDataList: res1 }));
        const res2 = await knowService.sensitiveDetectionList(collectionId, appId);
        set(() => ({ sensitiveDataList: res2 }));
        const res3 = await knowService.getSimilarityList(collectionId, appId);
        set(() => ({ repetitionDataList: res3.data || [] }));
        return;
      }

      if (Array.isArray(typeArr) && typeArr.length === 1) {
        const type = typeArr[0];
        if (typeof type === 'string' && type.includes('错别字')) {
          const res = await knowService.autoCorrectList(collectionId, appId);
          set(() => ({ wronglyDataList: res }));
          return;
        }

        if (typeof type === 'string' && type.includes('敏感词')) {
          const res = await knowService.sensitiveDetectionList(collectionId, appId);
          set(() => ({ sensitiveDataList: res }));
          return;
        }

        if (typeof type === 'string' && type.includes('重复')) {
          const res = await knowService.getSimilarityList(collectionId, appId);
          set(() => ({ repetitionDataList: res.data || [] }));
        }
      }
    } catch (error) {
      set(() => ({ checkoutList: [] }));
    }
  },
}));
